/**
 * 
 */
package com.nari.basic.utils.sysmgr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nari.basic.model.IMenu;
import com.nari.basic.model.IRole;
import com.nari.basic.model.IUser;
import com.nari.coreframe.webframe.SysEnvironmentMgr;
import com.nari.platform.model.WebConstants;

/**
 * 系统会话HttpSession、用户角色信息等的管理工具
 * @author Xiaopeng
 * @date 2013-7-22
 */
public class UserRoleUtil {
	public static final Integer sysPlatform_userRole_superMananger = 1; //超级管理员角色
	public static final Integer sysPlatform_userRole_commonUser = 2; //普通用户角色
	public static final String sysPlatform_superManager_roleID = "superManager"; //超级管理员角色编号
	
	/**
	 * 获取系统登录用户信息
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-22
	 */
	public static IUser getSysLoginUser() {
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		return (IUser) session.getAttribute(WebConstants.USER_SESSION);
	}
	
	/**
	 * 获取系统登录用户信息
	 * @param request
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-22
	 */
	public static IUser getSysLoginUser(HttpServletRequest request) {
		HttpSession session = SysEnvironmentMgr.getSessionInstance(request);
		return (IUser) session.getAttribute(WebConstants.USER_SESSION);
	}
	
	/**
	 * 获取登录用户的角色ID信息
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-22
	 */
	public static String getUserRoleIDs() {
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		return (String) session.getAttribute(WebConstants.USER_ROLEIDS);
	}
	
	/**
	 * 获取登录用户的角色ID信息
	 * @param request
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-22
	 */
	public static String getUserRoleIDs(HttpServletRequest request) {
		HttpSession session = SysEnvironmentMgr.getSessionInstance(request);
		return (String) session.getAttribute(WebConstants.USER_ROLEIDS);
	}
	
	/**
	 * 获取登录用户的角色信息
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-22
	 */
	@SuppressWarnings("unchecked")
	public static List<IRole> getUserRoles() {
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		return (List<IRole>) session.getAttribute(WebConstants.USER_ROLES);
	}
	
	/**
	 * 获取登录用户的角色信息
	 * @param request
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-22
	 */
	@SuppressWarnings("unchecked")
	public static List<IRole> getUserRoles(HttpServletRequest request) {
		HttpSession session = SysEnvironmentMgr.getSessionInstance(request);
		return (List<IRole>) session.getAttribute(WebConstants.USER_ROLES);
	}
	
	/**
	 * 将角色列表转化为roleID-roleName映射结构
	 * @param roleList
	 * @return
	 * @date 2012-9-14
	 */
	public static Map<String, String> formatRoleMap(List<IRole> roleList) {
		Map<String, String> roleMap = new HashMap<String, String>();
		if(roleList!=null && roleList.size()>0) {
			for(IRole role: roleList) 
				roleMap.put(role.getRoleID(), role.getRoleName());
		}
		return roleMap;
	}
	
	/**
	 * 格式化角色UUID信息
	 * @param roleList
	 * @return
	 * @date 2012-9-20
	 */
	public static String formatIDByRoleList(List<IRole> roleList) {
		StringBuffer idList = new StringBuffer("");
		if(roleList!=null && roleList.size()>0) {
			for(int index=0; index<roleList.size(); index++) {
				idList.append(roleList.get(index).getId());
				if(index<roleList.size()-1) 
					idList.append(",");
			}
		}
		return idList.toString();
	}
	
	/**
	 * 格式化角色ID信息
	 * @param roleList
	 * @return
	 * @date 2012-9-21
	 */
	public static String formatRoleIDByRoleList(List<IRole> roleList) {
		StringBuffer idList = new StringBuffer("");
		if(roleList!=null && roleList.size()>0) {
			for(int index=0; index<roleList.size(); index++) {
				idList.append(roleList.get(index).getRoleID());
				if(index<roleList.size()-1) 
					idList.append(",");
			}
		}
		return idList.toString();
	}
	
	/**
	 * 格式化角色名称信息
	 * @param roleList
	 * @return
	 * @date 2012-9-21
	 */
	public static String formatRoleNameByRoleList(List<IRole> roleList) {
		StringBuffer nameList = new StringBuffer("");
		if(roleList!=null && roleList.size()>0) {
			for(int index=0; index<roleList.size(); index++) {
				nameList.append(roleList.get(index).getRoleName());
				if(index<roleList.size()-1) 
					nameList.append(",");
			}
		}
		return nameList.toString();
	}
	
	/**
	 * 格式化菜单UUID信息
	 * @param menuList
	 * @return
	 * @author Xiaopeng
	 * @date 2013-7-23
	 */
	public static String formatIDByMenuList(List<IMenu> menuList) {
		StringBuffer idList = new StringBuffer("");
		if(menuList!=null && menuList.size()>0) {
			for(int index=0; index<menuList.size(); index++) {
				idList.append(menuList.get(index).getId());
				if(index<menuList.size()-1) 
					idList.append(",");
			}
		}
		return idList.toString();
	}
}
